﻿using B.S.DataBase.MyProject.ErrorCode;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.CarouselimageCommand;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MedicalPCTerminal.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarouselimageController : ControllerBase
    {
        private readonly IMediator mediator;

        public CarouselimageController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 轮播图新增
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> Carouselimage(CarouselimageCreateCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 上传轮播图图片
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns>图片访问URL</returns>
        [HttpPost]
        public async Task<IActionResult> UploadImage(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("未选择图片文件");
            }

            var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images");
            if (!Directory.Exists(uploadsFolder))
            {
                Directory.CreateDirectory(uploadsFolder);
            }
            var fileExt = Path.GetExtension(file.FileName);
            var fileName = $"carousel_{Guid.NewGuid()}{fileExt}";
            var filePath = Path.Combine(uploadsFolder, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var imageUrl = $"/images/{fileName}";
            return Ok(new { url = imageUrl });
        }

        /// <summary>
        /// 轮播图删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> CarouselimageDelete(CarouselimageDeleteCommand request)
        {
            try
            {
               return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 轮播图修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> CarouselimageUpdate(CarouselimageUpdateCommand request)
        {
            try
            {

                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
